
#include <assert.h>
#include "epoll_svr_reactor.h"
#include "svr_reactor_facade.h"

static CEpollServerReactor s_oReactor;


////////////////////////
// class CReactor
CReactor::CReactor()
{
}

CReactor::~CReactor()
{
}

int CReactor::Initialize(
						 bool bNeedTimer,
						 bool bNeedUDP,
						 bool bNeedTCPServer,
						 bool bNeedTCPClient,
						 bool bNeedUserAction)
{
	return s_oReactor.Initialize(
		bNeedTimer, 
		bNeedUDP, 
		bNeedTCPServer, 
		bNeedTCPClient, 
		bNeedUserAction);
}

/////////////////// UDP /////////////////////
int CReactor::RegisterUDPServer(
								int& iServerHandle, // [OUT]
								unsigned short ushPort, 
								const char* pszHost,
								bool bNoDelay,
								bool bNoFragment)
{
	return s_oReactor.RegisterUDPServer(
		iServerHandle, ushPort, pszHost, bNoDelay, bNoFragment);
}

int CReactor::RegisterPeerAddr(
							   int& iPeerHandle,   // [OUT]
							   const char* pszHost,
							   unsigned short ushPort)
{
	return s_oReactor.RegisterPeerAddr(
		iPeerHandle,   // [OUT]
		pszHost,
		ushPort);
}

int CReactor::SendTo(
					 int iServerHandle,
					 const char* pSendBuf, 
					 uint32_t nBufLen, 
					 int iPeerHandle,
					 int iFlag)
{
	return s_oReactor.SendTo(
		iServerHandle,
		pSendBuf, 
		nBufLen, 
		iPeerHandle,
		iFlag);
}

int CReactor::SendTo(
					 int iServerHandle,
					 const char* pSendBuf, 
					 uint32_t nBufLen, 
					 const char* pszHost,
					 unsigned short ushPort,
					 int iFlag)
{
	return s_oReactor.SendTo(
		iServerHandle,
		pSendBuf, 
		nBufLen, 
		pszHost,
		ushPort,
		iFlag);
}

void CReactor::RegisterUDPNetHandler(IUDPNetHandler* pHandler)
{
	return s_oReactor.RegisterUDPNetHandler(
		pHandler);
}

/////////////////// TCP /////////////////////
int CReactor::RegisterTCPServer(
								int& iServerHandle, // [OUT]
								ITCPNetHandler* pHandler,
								unsigned short ushPort, 
								int iRcvBufLen, // Default 128K
								const char* pszHost,
								bool bNoDelay)
{
	return s_oReactor.RegisterTCPServer(
								iServerHandle, // [OUT]
								pHandler,
								ushPort, 
								iRcvBufLen, // Default 128K
								pszHost,
								bNoDelay);
}

int CReactor::ConnectToServer(
							  ITCPNetHandler* pHandler,
							  unsigned short ushServerPort, 
							  const char* pszServerHost,	
							  ITCPNetHandler*& pOutHandler,
							  bool bIsAsync
							  )
{
	return s_oReactor.ConnectToServer(
		pHandler,
		ushServerPort, 
		pszServerHost,
		pOutHandler,
		bIsAsync
		);
}

uint32_t CReactor::Send(
					  int iConnHandle,
					  char* pSendBuf, 
					  uint32_t nBufLen, 
					  int iFlag)
{
	return s_oReactor.Send(
		iConnHandle,
		pSendBuf, 
		nBufLen, 
		iFlag);
}

int CReactor::Close(
					int iConnHandle)
{
	return s_oReactor.Close(
		iConnHandle);
}

void CReactor::NeedOnSendCheck(int iConnHandle)
{
	return s_oReactor.NeedOnSendCheck(iConnHandle);
}


/////////////////// Timer ///////////////////
int CReactor::RegisterTimer(
							int& iTimerID,              // [OUT]
							ITimerHandler* pHandler,
							const CTimeValue& tvInterval,
							unsigned int dwCount       // Count for timeout, 0 means endless loop   
							)
{
	return s_oReactor.RegisterTimer(
		iTimerID,              // [OUT]
		pHandler,
		tvInterval,
		dwCount       // Count for timeout, 0 means endless loop   
		);
}

int CReactor::UnregisterTimer(int iTimerID)
{
	return s_oReactor.UnregisterTimer(iTimerID);
}

/////////////////// User Event ///////////////////
void CReactor::RegisterUserEventHandler(
										IUserEventHandler* pUserEventHandler
										)
{
	return s_oReactor.RegisterUserEventHandler(
		pUserEventHandler
		);
}

/////////////////// Event Loop ///////////////////
void CReactor::RunEventLoop()
{
	return s_oReactor.RunEventLoop();
}

void CReactor::StopEventLoop()
{
	return s_oReactor.StopEventLoop();
}

/////////////////// Error Msg Description ///////////////////
const char* CReactor::GetLastErrMsg() const
{
	return s_oReactor.GetLastErrMsg();
}

uint32_t CReactor::CleanHandle(int iBeginHandle, int iStep)
{
	return s_oReactor.CleanHandle(iBeginHandle, iStep);
}

